package com.study.demo;

import java.net.URL;
import java.util.Random;

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;

/**动画示例 ，飘雪
*@description
*@author xtayhame
*date 2020年3月22日
*version
*since v1.0
*/
public class SnowAnimationMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		int totalSnow=80;  //总的雪花数量
		Duration time = Duration.seconds(9);
		Random random = new Random();
		ObservableList<ImageView> list=FXCollections.observableArrayList();
		StackPane pane = new StackPane();

		//pane.setStyle("-fx-background-image: url(/image/小木屋.jpg);");
		Image image2= new Image("/image/小木屋.jpg", stage.getWidth(), stage.getHeight(), false, true);
		BackgroundImage backgroundImage = new BackgroundImage(image2, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, BackgroundSize.DEFAULT);
		pane.setBackground(new Background(backgroundImage));

		stage.setScene(new Scene(pane,800,500));
		stage.setFullScreen(true);
		stage.setFullScreenExitHint("");
		stage.show();

		pane.setPrefSize(stage.getWidth(), stage.getHeight());

		System.out.println(stage.getWidth());
		//
		for(int i=0;i<totalSnow;i++){
			Image image = new Image("/icon/snow.png");
			ImageView imageView = new ImageView(image);
			//imageView.setStyle("-fx-background-color:#9400D00");
			imageView.setPreserveRatio(true);
			imageView.setFitWidth(30);

			imageView.setTranslateX(random.nextInt((int)stage.getWidth()));
			imageView.setTranslateY(random.nextInt((int)stage.getHeight()) -500);
			imageView.setTranslateZ(random.nextInt(1500));
			pane.getChildren().add(imageView);
			list.add(imageView);
		}


		//添加动画效果
		for(int i=0;i<list.size();i++){
			ImageView iv = list.get(i);

			TranslateTransition tt= new TranslateTransition(time);
			tt.setDuration(Duration.seconds(random.nextInt(10)).add(time));
			tt.setFromX(iv.getTranslateX());
			tt.setFromY(iv.getTranslateY());
			tt.setFromZ(iv.getTranslateZ());

			tt.setByX(50);
			tt.setByY(50);
			tt.setByZ(50);

			tt.setToX(iv.getTranslateX() + random.nextInt(500));
			tt.setToY(stage.getHeight() + 100);
			tt.setToZ(iv.getTranslateZ() + random.nextInt(500));

			FadeTransition  ft = new FadeTransition();
			ft.setFromValue(0);
			ft.setToValue(1);
			ft.setByValue(0.2);
			ft.setDuration(Duration.seconds(1));

			ParallelTransition pt = new ParallelTransition();
			pt.getChildren().addAll(tt,ft);
			pt.setNode(iv);
			pt.setCycleCount(Animation.INDEFINITE);

			pt.play();

		}

		URL url=this.getClass().getClassLoader().getResource("mp3/renzhendexue.mp3");
		System.out.println(url);
		Media media = new Media(url.toExternalForm());
		MediaPlayer player = new MediaPlayer(media);
		player.play();

	}
}
